package com.diablo.sitemanager.service.impl;

import com.diablo.sitemanager.dao.ArticleMapper;
import com.diablo.sitemanager.entity.Article;
import com.diablo.sitemanager.entity.ArticleVo;
import com.diablo.sitemanager.entity.QiniuInfo;
import com.diablo.sitemanager.service.ArticleService;
import com.diablo.sitemanager.util.QiniuUtil;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by diablo13064 on 2018/6/7.
 */
@Service("ArticleService")
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    ArticleMapper articleMapper;
    @Override
    public int addArticle(Article article) {
        return articleMapper.insertSelective(article);
    }

    @Override
    public int editArticle(Article article) {
        return articleMapper.updateByPrimaryKeyWithBLOBs(article);
    }

    @Override
    public int deleteArticle(Long articleId) {
        return articleMapper.deleteByPrimaryKey(articleId);
    }

    @Override
    public List<ArticleVo> findAllArticle(int pageNum, int pageSize) {
        if (pageNum != 0 && pageSize != 0) {
            PageHelper.startPage(pageNum, pageSize);
        }
        return articleMapper.selectAllAricle();
    }

    @Override
    public List<String> getImageList(String dir) {
        File file = new File(dir);
        List<String> list = new ArrayList<>();
        if (file.isDirectory()) {
            for (File f : file.listFiles()) {
                if (f.getName().startsWith("f")) {
                    list.add(f.getAbsolutePath());
                }
            }
        }
        return list;
    }
}
